package com;

import java.util.ArrayList;
import java.util.List;

import org.jgrapht.alg.DijkstraShortestPath;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleWeightedGraph;


public class CreateTSGraph {
	
	static SimpleWeightedGraph<Node, DefaultWeightedEdge> TSgraph;
	
	/*This method takes the graph of Cam plus alibi checker locations and creates and returns a new graph containing just the 
	locations as vertices, interconnected with each other, with weights being the shortest distance between them in the 
	original graph. To find the shortest distance, Dijkstra's algorithm is used. */
	
	static SimpleWeightedGraph<Node, DefaultWeightedEdge> create(SimpleWeightedGraph<Node, DefaultWeightedEdge> graph, ArrayList<Node> locationNodes) {
    	TSgraph = new SimpleWeightedGraph<Node, DefaultWeightedEdge>(DefaultWeightedEdge.class);
		for (Node noDe : locationNodes) {
			//first add all the locations as vertices in new graph.
			TSgraph.addVertex(noDe);}
		for (Node node1 : locationNodes) {
			for (Node node2 : locationNodes) {
				//for every pair of locations, find their shortest distance and add edge in new graph.
				//however: dont add an edge to yourself and dont add edge if one was created already (undirected graph).
				if (!node2.equals(node1) && !TSgraph.containsEdge(node1, node2)) {
											 DijkstraShortestPath<Node, DefaultWeightedEdge> dijk = new DijkstraShortestPath<Node, DefaultWeightedEdge>(graph, node1, node2);
											 double dist = dijk.getPathLength();
											 List<DefaultWeightedEdge> edgeList = dijk.getPathEdgeList();
											 //if a locationNode is disconnected from the graph, then get distances from Haversine formula (an underestimate)
											 if (edgeList == null) {dist = DistBetwNodes.get_distance(node1, node2);} else {}
											 TSgraph.addEdge(node1, node2);
	        								 DefaultWeightedEdge edge = TSgraph.getEdge(node1, node2);
	        								 TSgraph.setEdgeWeight(edge, dist);
	        								 //System.out.println(TSgraph.getEdgeWeight(edge));
				} else {}
			}
		}
		
		return TSgraph;
	}
}
